package com.company.vmpx.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import androidx.annotation.LayoutRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.dee.components.widget.verticalview.SimpleViewHolder;


public class PopMenu extends PopupWindow {
    private Context context;
    private View view;
    private View offsetview;
    private int offset_x;
    private int offset_y;
    private int gravity = Gravity.RIGHT | Gravity.TOP;

    public PopMenu(Context context, @LayoutRes int resource) {
        this.context = context;
        this.view = LayoutInflater.from(context).inflate(resource, null);
    }

    public PopMenu(Context context, @LayoutRes int resource, View offsetview, int offset_x, int offset_y) {
        this.offset_x = offset_x;
        this.offset_y = offset_y;
        this.offsetview = offsetview;
        this.context = context;
        this.view = LayoutInflater.from(context).inflate(resource, null);
    }

    public void init() {
        // 获取状态栏高度
        Rect frame = new Rect();
        // 状态栏高度 frame.top
        int xOffset = offset_x; // 设置x方向offset为5dp
        int yOffset = frame.top + offset_y; // 减去阴影宽度，适配UI
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 必须设计BackgroundDrawable后setOutsideTouchable(true)才会有效。这里在XML中定义背景，所以这里为null
        setBackgroundDrawable(new ColorDrawable(0000000000));
        setFocusable(true);
        setOutsideTouchable(true); // 点击外部关闭
        setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置Gravity,让它显示在右上角
        showAtLocation(offsetview, gravity, xOffset, yOffset);

    }

    public void setOffsetview(View offsetview) {
        this.offsetview = offsetview;
    }

    public void setOffset_x(int offset_x) {
        this.offset_x = offset_x;
    }

    public void setOffset_y(int offset_y) {
        this.offset_y = offset_y;
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    /**
     * ViewHolder，在 fillVerticalItem 方法中填充数据，加载标签背景图片等
     * 使用 ViewHolder 后 Adapter OnVerticalItemClick及在xml中设置的属性
     * 除isSetAnimDuration及interval 外 失效
     *
     * @param <T> item 数据模型
     */
    public interface ViewHolder<T> {
        void onHolder(SimpleViewHolder holder);
    }

    public void setMViewHolder(ViewHolder mViewHolder) {
        SimpleViewHolder viewHolder = new SimpleViewHolder(view);
        if (mViewHolder != null) {
            mViewHolder.onHolder(viewHolder);
        }
    }
}
